package com.koron.common.web.follow.service.impl;

import com.koron.bean.base.Response;
import com.koron.bean.util.ServletUtils;
import com.koron.common.core.business.datadict.utils.DataDictTool;
import com.koron.common.core.util.CodeTools;
import com.koron.common.web.follow.bean.followBean;
import com.koron.common.web.follow.mapper.FollowMapper;
import com.koron.common.web.follow.service.followService;
import lombok.extern.slf4j.Slf4j;
import org.koron.ebs.mybatis.SessionFactory;
import org.koron.ebs.mybatis.TaskAnnotation;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class followServiceImpl implements followService {


    @Override
    @TaskAnnotation("addfollow")
    public Response addfollow(SessionFactory factory, followBean follow) {
        if (follow.getPage_id() == null) return Response.fail("page_id出错！");
        else {
            FollowMapper mapper = factory.getMapper(FollowMapper.class);
            if (mapper.getfollow(follow) != null) return Response.fail("该页面已被关注！");
            else {
                follow.setId(CodeTools.getCode32());
                mapper.addfollow(follow);
                return Response.success("添加成功");
            }
        }
    }

    @Override
    @TaskAnnotation("cancelfollow")
    public Response cancelfollow(SessionFactory factory, followBean follow) {
        FollowMapper mapper = factory.getMapper(FollowMapper.class);
        mapper.cancelfollow(follow);
        return Response.success("取关成功！");
    }

    @Override
    @TaskAnnotation("serchfollow")
    public Response serchfollow(SessionFactory factory, followBean follow) {
        FollowMapper mapper = factory.getMapper(FollowMapper.class);
        String color = null;
        //把当前账号当前租户下的所有关注拿出来
        List<followBean> result = mapper.getfollowByAc(follow);
        if (result==null || result.size()==0)return Response.success(result);
        Map<String, String> colors = DataDictTool.getDataDictByCode("publicDictionary", "color");
        //对每个关注的页面遍历
        for (followBean a : result) {
            //把modole的key转换成value
            Map<String, String> list = DataDictTool.getDataDictByCode("publicDictionary", "follow_DB");
            String table = null;
            for (String s : list.keySet()) {
                if (s.equals(a.getModule())) {
                    table = list.get(s);
                    break;
                }
            }
            if (table == null) return Response.fail("module出错！");

            //根据表名去获取当前关注页面的状态
            followBean bean = mapper.getfollowlist(a, table);
            a.setTitle(bean.getTitle());
            a.setStatus(bean.getStatus());
            a.setName(bean.getName());
            a.setState(bean.getState());
            //特殊处理
            if (a.getModule().equals("sp_project_change1")) table="w_sp_project_change1";

            String language = ServletUtils.getRequest().getHeader("Language");
            log.info("语言头》》》》》："+language);
            if (a.getStatus() == null && a.getState() != null) {
                color = mapper.getcolor(a.getState(), table, language);
            } else if (a.getStatus() != null && a.getState() == null) {
                color = mapper.getcolor(a.getStatus(), table, language);
            }

            //根据状态获取颜色
            if (color != null) {
                a.setColor_type(color);
                for (String s : colors.keySet()) {
                    String[] statusList = colors.get(s).split(",");
                    for (String ss : statusList) {
                        if (color.equals(ss)) {
                            a.setColor(s);
                            break;
                        }
                    }
                    if (a.getColor() != null) break;
                }
            }
        }
        return Response.success(result);
    }

    @Override
    @TaskAnnotation("getfollow")
    public Response getfollow(SessionFactory factory, followBean follow) {
        FollowMapper mapper = factory.getMapper(FollowMapper.class);
        followBean result = mapper.getfollow(follow);
        return Response.success(result);
    }
}
